package persistence.user;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.User;

public class UserDAOFile implements UserDAO {

    private Map<String, User> userMap = new HashMap<String, User>();
    private String fileName;
    private static UserDAOFile persistenceManager = null;

    private UserDAOFile() {
    }

    public static UserDAO getJDBCPersistenceManager() {
        if (persistenceManager == null) {
            persistenceManager = new UserDAOFile();
        }
        return persistenceManager;
    }

    @Override
    public boolean setUp(String url, String driver, String user, String password) {
        this.fileName = "fileName";
        try {
            File fileUser = new File(fileName + "usuarios.txt");
            if (!fileUser.exists()) {
                return true;
            }
            InputStream inputStream = new FileInputStream(fileUser);
            ObjectInputStream objectInputStreamUsuarios = new ObjectInputStream(inputStream);
            int numberOfUsers = (Integer) objectInputStreamUsuarios.readObject();
            for (int i = 0; i < numberOfUsers; i++) {
                User usuario = (User) objectInputStreamUsuarios.readObject();
                userMap.put(usuario.getUsuario(), usuario);
            }
            objectInputStreamUsuarios.close();
        } catch (Exception ex) {
            Logger.getLogger(UserDAOFile.class.getName()).log(Level.SEVERE,
                    "No se pudieron cargar los datos del archivo de usuarios", ex);
            return false;
        }

        return true;
    }

    @Override
    public boolean disconnect() {

        try {
            OutputStream outputStreamUsers = new FileOutputStream(new File(fileName + "usuarios.txt"));
            ObjectOutputStream objectOutputStreamUsers = new ObjectOutputStream(outputStreamUsers);
            objectOutputStreamUsers.writeObject(userMap.size());
            for (User user : userMap.values()) {
                objectOutputStreamUsers.writeObject(user);
            }
            objectOutputStreamUsers.close();
        } catch (IOException ex) {
            Logger.getLogger(UserDAOFile.class.getName()).log(Level.SEVERE,
                    "No se pudieron guardar los datos en el archivo de usuarios", ex);
            return false;
        }

        return true;
    }

    @Override
    public synchronized boolean createUser(User user) {
        if (userMap.containsKey(user.getUsuario())) {
            return false;
        } else {
            userMap.put(user.getUsuario(), user);
            Logger.getLogger(UserDAOFile.class.getName()).log(Level.SEVERE,
                    "No se pudieron cargar los datos del archivo de usuarios");
            return true;
        }
    }

    @Override
    public synchronized boolean deleteUser(String name) {
        if (!userMap.containsKey(name)) {
            return false;
        } else {
            userMap.remove(name);
            return true;
        }
    }

    @Override
    public synchronized boolean updateUser(User user) {
        if (!userMap.containsKey(user.getUsuario())) {
            return false;
        } else {
            userMap.put(user.getUsuario(), user);
            return true;
        }
    }

    @Override
    public synchronized User confirmUser(String name, String password) {
        User user = userMap.get(name);
        if (user == null) {
            return user;
        }
        if (password.equals(user.getPassword())) {
            user.setUsuario(name);
            return user;
        }
        return null;
    }

    @Override
    public synchronized User readUser(String name) {
        return userMap.get(name);
    }
}
